package com.css.fxfzypg.modules.baseData.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.baseData.dto.PeoDTO;
import com.css.fxfzypg.modules.baseData.queryParams.PeoQueryParams;
import com.css.fxfzypg.modules.baseData.entity.PeoEntity;
import com.css.fxfzypg.modules.baseData.service.PeoService;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-08-11 14:45
 * 人口普查数据子模块
 */
@RestController
@RequestMapping("/peo")
@Slf4j
public class PeoController {

    @Resource
    private PeoService peoService;

    /**
     * 查看
     * @param id
     * @return
     */
    @GetMapping("/getPeoById")
    public RestResponse getPeoById(String id){
        RestResponse restResponse=null;
        try {
            PeoEntity peoEntity=  peoService.findPeoById(id);
            restResponse = RestResponse.succeed(peoEntity);
        } catch (Exception e) {
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 分页查询
     * @param request
     * @param queryParams
     * @return
     */
    @GetMapping("/getAllPeo")
    public RestResponse getAllPeo(HttpServletRequest request, PeoQueryParams queryParams){
        RestResponse restResponse = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=peoService.getAllPeoByParams(queryParams,curPage,pageSize);
            restResponse = RestResponse.succeed(page);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改
     * @param peoDTO
     * @return
     */
    @PostMapping("/updatePeo")
    public RestResponse updatePeo(@RequestBody PeoDTO peoDTO){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try {
            peoService.updatePeo(peoDTO);
            jsonObject.put("message","修改成功");
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 删除
     * @param
     * @return
     */
    @PostMapping("/deletePeo/{ids}")
    public RestResponse deleteTask(@PathVariable("ids") String ids){
        RestResponse response = null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] arr= ids.split(",");
            List<String> list = Arrays.asList(arr);
            peoService.deletePeo(list);
            jsonObject.put("message","删除完成");
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 导入
     * @param file
     * @return
     */
    @PostMapping("/peoImportExcel")
    public RestResponse importExcel(MultipartFile file){
        RestResponse response = null;
        try {
            String info=peoService.importExcel(file);
            response = RestResponse.succeed(info);
        } catch (Exception e) {
            String errormessage = "导入失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

}
